package com.handa.admin.userservice.common.security.service;

import com.handa.admin.userservice.common.entity.LoginUser;
import com.handa.admin.userservice.entity.DO.SysUserEntity;
import com.handa.admin.userservice.service.impl.UserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@RequiredArgsConstructor
public class MyUserDetailService implements UserDetailsService {

    @Resource
    private UserServiceImpl userServiceImpl;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUserEntity user = userServiceImpl.getUserByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("User not found");
        }


        return LoginUser.builder()
                .username(user.getUsername())
                .passwd(user.getPassword())
                .roleId(user.getRoleId())
                .userId(user.getUserId())
                .nickname(user.getNickname())
                .build();
    }
}
